package sk.tuke.constraintbuilder.views.components;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import sk.tuke.constraintbuilder.model.Annotation;
import sk.tuke.constraintbuilder.model.Context;
import sk.tuke.constraintbuilder.model.ILeaf;
import sk.tuke.constraintbuilder.model.StructuredRuleElement;

public class AnnotationContainerDialog extends Dialog {
	
	//Element - Annotation_Container
	StructuredRuleElement element;
	
	//GUI Components
	private Text minOccursField;
	private Text maxOccursField;
	private Combo contextCombo;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public AnnotationContainerDialog(Shell parentShell, StructuredRuleElement element) {
		super(parentShell);
		this.element = element;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Context: ");
		
		contextCombo = new Combo(container, SWT.READ_ONLY);
		contextCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		contextCombo.setItems(new String[] {"METHOD", "FIELD", "PARAMETER", "TYPE", "PACKAGE", "CONSTRUCTOR", " "});
		contextCombo.select(6);
		
		Label lblNewLabel_1 = new Label(container, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("Min.occurs: ");
		
		minOccursField = new Text(container, SWT.BORDER);
		minOccursField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_2 = new Label(container, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText("Max.occurs: ");
		
		maxOccursField = new Text(container, SWT.BORDER);
		maxOccursField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		//Only numbers
		minOccursField.addListener(SWT.Verify, new Listener() {
			public void handleEvent(Event e) {
				String string = e.text;
				char[] chars = new char[string.length()];
				string.getChars(0, chars.length, chars, 0);
				for (int i = 0; i < chars.length; i++) {
					if (!('0' <= chars[i] && chars[i] <= '9')) {
						e.doit = false;
						return;
					}
				}
			}
	    });
		maxOccursField.addListener(SWT.Verify, new Listener() {
			public void handleEvent(Event e) {
				String string = e.text;
				char[] chars = new char[string.length()];
				string.getChars(0, chars.length, chars, 0);
				for (int i = 0; i < chars.length; i++) {
					if (!('0' <= chars[i] && chars[i] <= '9')) {
						e.doit = false;
						return;
					}
				}
			}
	    });
		
		showElementDetails(element);

		return container;
	}
	
	private void showElementDetails(StructuredRuleElement element){
		ILeaf leaf;
		if(!element.getLeaves().isEmpty()){
			leaf = element.getLeaves().get(0);
			Integer minOccurs = null;
			Integer maxOccurs = null;
			Context context = null;
			if(leaf instanceof Annotation){
				minOccurs = ((Annotation) leaf).getMinOccurs();
				maxOccurs = ((Annotation) leaf).getMaxOccurs();
				context = ((Annotation) leaf).getContext();
			}
	
			if(minOccurs!=null){
				minOccursField.setText(String.valueOf(minOccurs));
			}
			if (maxOccurs!=null) {
				maxOccursField.setText(String.valueOf(maxOccurs));
			}
			if(context!=null){
				contextCombo.select(contextCombo.indexOf(context.name()));
			}
		}
	}
	
	@Override
	protected void okPressed() {
		Integer minOccurs = null;
		Integer maxOccurs = null;
		try{
			minOccurs = Integer.parseInt(minOccursField.getText());
			maxOccurs = Integer.parseInt(maxOccursField.getText());
		}catch(NumberFormatException e){}
		Context context = Context.getContext(contextCombo.getText());
		if(minOccurs!=null){
			for (ILeaf leaf : element.getLeaves()) {
				if(leaf instanceof Annotation)
					((Annotation) leaf).setMinOccurs(minOccurs);
			}
		}
		if (maxOccurs!=null) {
			for (ILeaf leaf : element.getLeaves()) {
				if(leaf instanceof Annotation)
					((Annotation) leaf).setMaxOccurs(maxOccurs);
			}
		}
		if(context!=null){
			for (ILeaf leaf : element.getLeaves()) {
				if(leaf instanceof Annotation)
					((Annotation) leaf).setContext(context);
			}
		}
		super.okPressed();
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
		createButton(parent, IDialogConstants.CANCEL_ID,IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(278, 170);
	}

}
